package com.portal.gui;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.portal.gui.marketman.MarketingManPortal;
import com.portal.gui.marketmanger.MarketManagerPortal;
import com.portal.gui.salesman.SalesmanPortal;
import com.portal.logic.Salesman;
import com.portal.logic.SalesmanDAO;
import com.portal.logic.SerializationUtil;
import java.awt.Font;

public class GeneralPortal extends JFrame {

	SalesmanDAO salesmanDAO = null;
	Salesman salesman = null;
	private JTextField dbField;
	private JTextField usernameField;
	private JPasswordField dbPswrdField;

	public GeneralPortal() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 407, 333);
		setTitle("Employee Portal");
		getContentPane().setLayout(null);


		JLabel ipLabel = new JLabel("Ip");
		ipLabel.setBounds(113, 13, 34, 16);
		getContentPane().add(ipLabel);

		final JTextField ipField = new JTextField("localhost");
		//ipField = new JTextField("localhost");//127.0.0.1
		//ipField.setText("localhost");
		ipField.setBounds(163, 13, 121, 22);
		getContentPane().add(ipField);
		ipField.setColumns(10);



		//Name Label
		final JLabel nameLabel = new JLabel("Worker Name");
		nameLabel.setBounds(75, 155, 107, 20);
		getContentPane().add(nameLabel);

		//Name Text
		final JTextField nameField = new JTextField(10);
		nameField.setBounds(167, 155, 121, 20);
		getContentPane().add(nameField);

		//Pswrd Label
		final JLabel pswrdLabel = new JLabel("Worker Pswrd");
		pswrdLabel.setBounds(75, 175, 107, 20);
		getContentPane().add(pswrdLabel);

		//Pswrd Text
		final JPasswordField pswrdWorkerField = new JPasswordField(10);
		pswrdWorkerField.setBounds(167, 175, 121, 20);
		getContentPane().add(pswrdWorkerField);

		//Pswrd Label
		final JLabel typeLabel = new JLabel("Worker Type");
		typeLabel.setBounds(75, 195, 107, 20);
		getContentPane().add(typeLabel);

		// Role Combobox
		String[] workerTypeStrings = {"MarketingManager", "MarketingMan", "Salesman"};
		final JComboBox comboBox = new JComboBox(workerTypeStrings);
		comboBox.setSelectedIndex(2);
		comboBox.setBounds(167, 195, 121, 20);
		getContentPane().add(comboBox);

		//Button Login
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String workerName = nameField.getText();
				String workerPswrd = pswrdWorkerField.getText();
				String selectedWorkerType = (String)comboBox.getSelectedItem();
				System.err.println(workerName+","+workerPswrd+","+selectedWorkerType);

				String ip = ipField.getText();
				String db = dbField.getText();
				String username = usernameField.getText();
				String password = dbPswrdField.getText();
				try {
					salesmanDAO = new SalesmanDAO(ip, db, username, password);
				} catch (Exception e1) {
					//e1.printStackTrace();
					JOptionPane.showMessageDialog(null, e1.getLocalizedMessage());
					
					//Throwable throwable = e1.getCause();
					/*if(throwable instanceof SQLException){
						JOptionPane.showMessageDialog(null, "Failed to create the database connection.");
					}else if(throwable instanceof ClassNotFoundException){
						JOptionPane.showMessageDialog(null, "Driver not found.");
					}else{
						JOptionPane.showMessageDialog(null, "Connection Failed");
					}*/
				}
				salesman = salesmanDAO.selectByNameAndPswrd(workerName, workerPswrd);
				if(!selectedWorkerType.equalsIgnoreCase(salesman.getRole())) {
					JOptionPane.showMessageDialog(null, "Worker type of this salesman not match by Portal.");
				}else if(salesman.getIsConnected() == 1){
					JOptionPane.showMessageDialog(null, "Salesman already connected to Portal.");
				}else{

					//TODO add validation to all fields
					salesman.setIsConnected(1);
					try {
						SerializationUtil.serialize(salesman, "c:\\editSalesman.out");
					} catch (IOException e) {
						e.printStackTrace();
					}
					salesmanDAO.update();

					switch (selectedWorkerType) {
					case "Salesman":
						SalesmanPortal salesmanPortalFrame = new SalesmanPortal();
						salesmanPortalFrame.setVisible(true);
				/*		
						salesmanPortalFrame.btnBack.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent arg0) {
							this.setVisible(true);
							
						}
					});*/
						break;
					case "MarketingMan":
						MarketingManPortal marketingManPortalFrame = new MarketingManPortal();
						marketingManPortalFrame.setVisible(true);
						break;
					case "MarketingManager":
						MarketManagerPortal marketManagerPortalFrame = new MarketManagerPortal();
						marketManagerPortalFrame.setVisible(true);
						break;
					default:
						break;
					}
				}
			}

		});
		btnLogin.setBounds(85, 240, 93, 23);
		getContentPane().add(btnLogin); 

		final JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(salesman != null){
					salesman.setIsConnected(0);

					try {
						SerializationUtil.serialize(salesman, "c:\\editSalesman.out");
					} catch (IOException e) {
						e.printStackTrace();
					}
					salesmanDAO.update();
				}
				Container frame = btnExit.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});

		btnExit.setBounds(229, 239, 97, 25);
		getContentPane().add(btnExit);

		JLabel lblDb = new JLabel("DB");
		lblDb.setBounds(113, 42, 34, 16);
		getContentPane().add(lblDb);

		dbField = new JTextField("EmployeePortalDb");
		dbField.setBounds(163, 39, 121, 22);
		getContentPane().add(dbField);
		dbField.setColumns(10);

		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(79, 71, 72, 16);
		getContentPane().add(lblUsername);

		usernameField = new JTextField("root");
		usernameField.setBounds(163, 68, 121, 22);
		getContentPane().add(usernameField);
		usernameField.setColumns(10);

		JLabel lblPswrd = new JLabel("DB Pswrd");
		lblPswrd.setBounds(85, 100, 56, 16);
		getContentPane().add(lblPswrd);

		dbPswrdField = new JPasswordField("Braude");//Braude
		dbPswrdField.setBounds(163, 97, 121, 22);
		getContentPane().add(dbPswrdField);
		dbPswrdField.setColumns(10);
		
		JLabel lblCraudeSystem = new JLabel("Craude System\u00A9");
		lblCraudeSystem.setFont(new Font("Tahoma", Font.ITALIC, 8));
		lblCraudeSystem.setBounds(10, 269, 107, 14);
		getContentPane().add(lblCraudeSystem);


	}
}
